//
//  THGradientLabel.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THGradientLabel.h"

@interface THGradientLabel()

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@end

@implementation THGradientLabel

/**子类添加控件 */
-(void)setupUI
{
    [self addSubview:self.contentLabel];
    [self.layer addSublayer:self.gradientLayer];
}

/**子类控件属性值配置 */
-(void)configViews
{
    
}

/**子类控件frame配置 */
-(void)setupFrames
{
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
}

//待优化
- (void)layoutSubviews
{
    [super layoutSubviews];

}


- (void)setColors:(NSArray<UIColor *> *)colors
{
    NSMutableArray *colorArray = @[].mutableCopy;
    for (UIColor *color in colors) {
        [colorArray addObject:(__bridge id)color.CGColor];
    }
    [self.gradientLayer setColors:colorArray];

}

- (void)setText:(NSString *)text
{
    _text = text;
    _contentLabel.text = text;
    self.size = _contentLabel.compressedSize;
    if (@available(iOS 10.0, *)) {
        self.gradientLayer.mask = self.contentLabel.layer;
        self.gradientLayer.frame = self.bounds;
    }else{
        _contentLabel.textColor = kThemeColor;
    }
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    _contentLabel.font = font;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    _contentLabel.textColor = textColor;
}


- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = Color_text_33;
        _contentLabel.font = kFontRegular(14);
//        _contentLabel.backgroundColor = [UIColor clearColor];
    }
    return _contentLabel;
}

- (CAGradientLayer *)gradientLayer
{
    if (!_gradientLayer) {
        _gradientLayer = [CAGradientLayer layer];
        [_gradientLayer setStartPoint:CGPointMake(0.0, 0.0)];
        [_gradientLayer setEndPoint:CGPointMake(0.0, 1.0)];
    }
    return _gradientLayer;
}

@end
